package com.su.Servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class SessionDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //解决乱码问题
        req.setCharacterEncoding("UTF-8");
//        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

      //获得Session
        HttpSession session = req.getSession();
        //往Session中存东西
        session.setAttribute("name","张三");
        //获取Session的ID
        String sessionid = session.getId();

        //判断session是不是新建
        if(session.isNew()){
            resp.getWriter().write("session创建成功"+ sessionid);

        }else{
            resp.getWriter().write("session已经存在"+ sessionid);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
